# ~*~ coding: utf-8 ~*~

import unittest
import sys

from apps.ops.utils import update_or_create_ansible_task, run_ansible_task

sys.path.insert(0, "../..")

# 测试必须使用flask shell


class TestCreateOrUpdateAnsibleTask(unittest.TestCase):
    def setUp(self):
        pass

    def test_run(self):
        """
        ~]# export FLASK_APP=apps/manage.py
        ~]# flask shell
        Instance: /opt/example_flask/bees2/instance
        >>> from apps.ops.test import TestCreateOrUpdateAnsibleTask
        >>> ut=TestCreateOrUpdateAnsibleTask()
        >>> ut.test_run()
        :return:
        """

        task_name = 'test-create-ansible-task'
        hosts = [
            {"hostname": "testserver", 'ip': '172.16.10.10', "port": 22},
        ]
        tasks = [
            # {'name': "setup", 'action': {'module': 'setup'}},
            {"name": "run_cmd", "action": {"module": "shell", "args": "ls"}},
            {"name": "run_whoami", "action": {"module": "shell", "args": "whoami"}},
        ]
        create_by = "system"
        run_as = [
            {
                "username": "sa",
                "password": "sa123",
                "private_key": ""
            }
        ]
        run_as_admin = True
        become = [
            {
                'method': 'sudo',
                'user': 'root',
                'pass': '123456',
            }
        ]
        pattern = 'all'
        options = {}
        task = update_or_create_ansible_task(task_name=task_name, create_by=create_by, hosts=hosts,
                                             tasks=tasks, pattern=pattern, options=options,
                                             run_as=run_as, become=become, run_as_admin=run_as_admin)
        task.run()


class TestRunAnsibleTask(unittest.TestCase):
    def setUp(self):
        pass

    def test_run(self):
        """
        ~]# export FLASK_APP=apps/manage.py
        ~]# flask shell
        Instance: /opt/example_flask/bees2/instance
        >>> from apps.ops.test import TestRunAnsibleTask
        >>> ut=TestRunAnsibleTask()
        >>> ut.test_run()
        :return:
        """

        task_name = 'test-create-ansible-task'
        hosts = [
            {"hostname": "testserver", 'ip': '172.16.10.10', "port": 22},
        ]
        tasks = [
            # {'name': "setup", 'action': {'module': 'setup'}},
            {"name": "run_cmd", "action": {"module": "shell", "args": "ls"}},
            {"name": "run_whoami", "action": {"module": "shell", "args": "whoami"}},
        ]
        run_as = [
            {
                "username": "sa",
                "password": "sa123",
                "private_key": ""
            }
        ]
        run_as_admin = True
        become = [
            {
                'method': 'sudo',
                'user': 'root',
                'pass': '123456',
            }
        ]
        pattern = 'all'
        options = {}
        run_ansible_task(task_name=task_name, hosts=hosts, tasks=tasks,
                         pattern=pattern, options=options, run_as=run_as,
                         become=become, run_as_admin=run_as_admin)
